#coding=UTF-8
from PyQt4 import QtCore, QtGui
import IC_DefineLibrary
from Modify_UserInformation import *

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_Register_Dialog(QtGui.QMainWindow):  #object
    def setupUi1(self):   #Dialog1
        QtGui.QWidget.__init__(self)
        self.setObjectName(_fromUtf8("Dialog1"))
        self.setGeometry(QtCore.QRect(30, 30, 500, 260))
        self.resize(500, 260)
        self.frame_5 = QtGui.QFrame(self)
        self.frame_5.setGeometry(QtCore.QRect(0, 0, 500, 260))
        self.frame_5.setFrameShape(QtGui.QFrame.StyledPanel)
        self.frame_5.setFrameShadow(QtGui.QFrame.Raised)
        self.frame_5.setObjectName(_fromUtf8("frame_5"))
        self.frame_6 = QtGui.QFrame(self.frame_5)
        self.frame_6.setGeometry(QtCore.QRect(0, 0, 500, 70))
        self.frame_6.setFrameShape(QtGui.QFrame.StyledPanel)
        self.frame_6.setFrameShadow(QtGui.QFrame.Raised)
        self.frame_6.setObjectName(_fromUtf8("frame_6"))
        self.label_5 = QtGui.QLabel(self.frame_6)
        self.label_5.setGeometry(QtCore.QRect(0, 0, 500, 70))
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Arial"))
        font.setPointSize(10)
        font.setBold(True)
        font.setWeight(75)
        self.label_5.setFont(font)
        self.label_5.setFrameShape(QtGui.QFrame.StyledPanel)
        self.label_5.setAlignment(QtCore.Qt.AlignCenter)
        self.label_5.setObjectName(_fromUtf8("label_5"))
        self.frame_7 = QtGui.QFrame(self.frame_5)
        self.frame_7.setGeometry(QtCore.QRect(0, 70, 500, 170))
        self.frame_7.setFrameShape(QtGui.QFrame.StyledPanel)
        self.frame_7.setFrameShadow(QtGui.QFrame.Raised)
        self.frame_7.setObjectName(_fromUtf8("frame_7"))
        self.label_6 = QtGui.QLabel(self.frame_7)
        self.label_6.setGeometry(QtCore.QRect(90, 25, 100, 30))
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Arial"))
        font.setPointSize(9)
        font.setBold(True)
        font.setWeight(75)
        self.label_6.setFont(font)
        self.label_6.setAlignment(QtCore.Qt.AlignCenter)
        self.label_6.setObjectName(_fromUtf8("label_6"))
        self.label_7 = QtGui.QLabel(self.frame_7)
        self.label_7.setGeometry(QtCore.QRect(90, 75, 100, 30))
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Arial"))
        font.setPointSize(9)
        font.setBold(True)
        font.setWeight(75)
        self.label_7.setFont(font)
        self.label_7.setAlignment(QtCore.Qt.AlignCenter)
        self.label_7.setObjectName(_fromUtf8("label_7"))
        self.textEdit_3 = QtGui.QTextEdit(self.frame_7)
        self.textEdit_3.setGeometry(QtCore.QRect(190, 25, 200, 30))
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Arial"))
        font.setPointSize(9)
        self.textEdit_3.setFont(font)
        self.textEdit_3.setTabChangesFocus(True)
        self.textEdit_3.setObjectName(_fromUtf8("textEdit_3"))
        self.lineEdit_2 = QtGui.QLineEdit(self.frame_7)
        self.lineEdit_2.setGeometry(QtCore.QRect(190, 75, 200, 30))
        self.lineEdit_2.setEchoMode(QtGui.QLineEdit.Password)
        self.lineEdit_2.setObjectName(_fromUtf8("lineEdit_2"))
        self.pushButton_4 = QtGui.QPushButton(self.frame_7)
        self.pushButton_4.setGeometry(QtCore.QRect(190, 125, 80, 30))
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Arial"))
        font.setBold(True)
        font.setWeight(75)
        self.pushButton_4.setFont(font)
        self.pushButton_4.setObjectName(_fromUtf8("pushButton_4"))
        QtCore.QObject.connect(self.pushButton_4, QtCore.SIGNAL("clicked()"),self.SubmitData)  
        self.pushButton_5 = QtGui.QPushButton(self.frame_7)
        self.pushButton_5.setGeometry(QtCore.QRect(280, 125, 110, 30))
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Arial"))
        font.setBold(True)
        font.setWeight(75)
        self.pushButton_5.setFont(font)
        self.pushButton_5.setObjectName(_fromUtf8("pushButton_5"))
        QtCore.QObject.connect(self.pushButton_5, QtCore.SIGNAL("clicked()"),self.ModifyPassword )
        self.pushButton_6 = QtGui.QPushButton(self.frame_7)
        self.pushButton_6.setGeometry(QtCore.QRect(400, 125, 80, 30))
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Arial"))
        font.setBold(True)
        font.setWeight(75)
        self.pushButton_6.setFont(font)
        self.pushButton_6.setObjectName(_fromUtf8("pushButton_6"))
        QtCore.QObject.connect(self.pushButton_6, QtCore.SIGNAL("clicked()"), self.close)
        self.frame_8 = QtGui.QFrame(self.frame_5)
        self.frame_8.setGeometry(QtCore.QRect(0, 240, 500, 20))
        self.frame_8.setFrameShape(QtGui.QFrame.StyledPanel)
        self.frame_8.setFrameShadow(QtGui.QFrame.Plain)
        self.frame_8.setObjectName(_fromUtf8("frame_8"))
        
        self.retranslateUi1()
        QtCore.QMetaObject.connectSlotsByName(self)
        IC_DefineLibrary.MessageBox(title='Notes',message='Please fill your Username and Password infomation, then submit them to database !')

    def retranslateUi1(self): #Dialog1
        self.setWindowTitle(_translate("Dialog1", "User Register Centre", None))
        self.label_5.setText(_translate("Dialog1", "User Register Centre", None))
        self.label_6.setText(_translate("Dialog1", "Username", None))
        self.label_7.setText(_translate("Dialog1", "Password", None))
        self.pushButton_4.setText(_translate("Dialog1", "Submit", None))
        self.pushButton_5.setText(_translate("Dialog1", "Modify Password", None))
        self.pushButton_6.setText(_translate("Dialog1", "Close", None))

    def SubmitData(self):
        if self.textEdit_3.toPlainText()=='' or self.lineEdit_2.text()=='':
            return IC_DefineLibrary.MessageBox(title='Notes',message='Please input your new username and password !')
        te2=self.textEdit_3.toPlainText()
        le1=self.lineEdit_2.text()
        self.textEdit_3.setPlainText("")
        self.lineEdit_2.clear()
        dbo3=IC_DefineLibrary.DB_Operating(sql="Select Username, Password from Login")
        for (Username, Password) in dbo3.row:
            if te2==Username:
                return IC_DefineLibrary.MessageBox(title='Notes',message='This username has already existed now, please input new username !')
        dbo4=IC_DefineLibrary.DB_Operating(sql="insert into Login(Username,Password) values('"+'%s'%te2+"','"+'%s'%le1+"')")
#        dbo5=IC_DefineLibrary.DB_Operating(sql="insert into User_Login_Counts(English_Name,Login_Succeed_Counts,Login_Fail_Counts) values('"+'%s'%te2+"',0,0)")
        IC_DefineLibrary.MessageBox(title='Notes',message='You have registerred your information successfully !') 

    def ModifyPassword(self):
        self.myapps2=Ui_Modify_Dialog()
        self.myapps2.setupUi2()
        self.myapps2.show()
 
